package org.br.erp.rfq.quo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.br.erp.base.utils.ERPUtils;
import org.br.erp.rfq.quo.entity.Scmquotation;
import org.br.erp.rfq.quo.mapper.ScmquotationMapper;
import org.br.erp.rfq.quo.service.IScmquotationService;
import org.br.erp.rfq.xbj.entity.Scmxjd;
import org.br.erp.rfq.xbj.entity.Scmxjditem;
import org.br.erp.rfq.xbj.service.IScmxjdService;
import org.br.erp.rfq.xbj.service.IScmxjditemService;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.base.mapper.BaseCommonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Description: 报价单
 * @Author: jeecg-boot
 * @Date:   2023-09-17
 * @Version: V1.0
 */
@Service
public class ScmquotationServiceImpl extends ServiceImpl<ScmquotationMapper, Scmquotation> implements IScmquotationService {

    @Autowired
    IScmxjditemService scmxjditemService;

    @Autowired
    IScmxjdService scmxjdService;

    @Autowired
    ScmquotationMapper scmquotationMapper;

    @Autowired
    private BaseCommonMapper baseCommonMapper;


    @Override
    @Transactional
    public void gengerateQuo(List<Map> idMap) {

        //根据id查找来生成对应的报价单
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        String selecttablevalue = baseCommonMapper.selecttablevalue("sys_user", "innervendorid", "id", sysUser.getId());

        if(selecttablevalue == null || selecttablevalue.equalsIgnoreCase("")){
            throw new RuntimeException("当前用户没有定义报价供应商");
        }

        for (Map map : idMap) {
            String itemid = map.get("itemid").toString();
            String[] split = itemid.split("&");
            String xjditemid = split[1];
            Scmxjditem scmxjditem = scmxjditemService.getById(xjditemid);
            if(scmxjditem!=null){
                QueryWrapper<Scmquotation> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("fromitemid",scmxjditem.getId());
                queryWrapper.eq("fromid",scmxjditem.getParentid());
                queryWrapper.eq("quality",split[2]);
                queryWrapper.eq("vendorid",selecttablevalue);
                List<Scmquotation> scmquotations = scmquotationMapper.selectList(queryWrapper);
                for (Scmquotation scmquotation : scmquotations) {
                    scmquotation.setQuostate("0");
                    scmquotationMapper.updateById(scmquotation);
                }
                Scmquotation scmquotation=new Scmquotation();
                String matcode = scmxjditem.getMatcode();
                String matname = scmxjditem.getMatname();
                scmquotation.setCount(scmxjditem.getCount());
                scmquotation.setMatcode(matcode);
                scmquotation.setMatname(matname);
//                scmquotation.setVendorid(map.get("vendorid").toString());
                scmquotation.setVendorid(selecttablevalue);
                scmquotation.setQuality(split[2]);
                scmquotation.setQuodate(ERPUtils.getLocalDate());
                scmquotation.setFromid(split[0]);
                scmquotation.setFromitemid(split[1]);
                scmquotation.setQuoprice(ERPUtils.getBigDecimal(map.get("quoprice").toString()));
                BigDecimal quovalue = ERPUtils.getBigDecimal(map.get("quoprice").toString()).multiply(ERPUtils.getBigDecimal(scmquotation.getCount()));
                scmquotation.setQuovalue(quovalue);
                scmquotation.setQuoremarks(map.containsKey("quoremarks") ? map.get("quoremarks").toString() : "");
                scmquotation.setQuostate("1");
                scmquotationMapper.insert(scmquotation);
            }

            if(scmxjditem!=null){
                scmxjditem.setQuoflag("1");
                scmxjditemService.updateById(scmxjditem);
                Scmxjd scmxjd = scmxjdService.getById(scmxjditem.getParentid());
                scmxjd.setStatus("7");
                scmxjdService.updateById(scmxjd);
            }


        }
    }
}
